package com.guanglan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guanglan.DTO.RoleOnUser;
import com.guanglan.entity.SysRole;
import com.guanglan.entity.SysUser;
import com.guanglan.service.SysRoleService;
import com.guanglan.service.SysUserService;
import com.guanglan.utils.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 作者: 光蓝Comet
 * Date: 2018-06-20 15:29
 * Description: 一句话说明这个类的作用
 * Since 1.0
 */
@RequestMapping(value = "/system/user")
@Controller
public class SysUserController {

    private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping(value = "/index")
    public String index(@RequestParam(name = "userName",defaultValue = "",required = false) String userName,
                        @RequestParam(name = "userPhone",defaultValue = "",required = false) String userPhone,
                        @RequestParam(value="nowPage",required=false,defaultValue="1") Integer nowPage,
                        @RequestParam(value="pageSize",required=false,defaultValue="10") Integer pageSize,
                        Model model){
        logger.info("Current user into the user method!");
        PageHelper.startPage(nowPage, pageSize);
        PageHelper.orderBy("create_time desc");
        List<SysUser> users = sysUserService.selectSysUsers(userName,userPhone);
        PageInfo<SysUser> pageInfo = new PageInfo<>(users);
        model.addAttribute("pageInfo",pageInfo);
        return "user/userIndex";
    }

    @RequestMapping(value = "/editPage")
    public String editPage(@RequestParam(value="editId",required=false) String editId,Model model){
        if (StringUtils.trimToNull(editId) != null){
            SysUser sysUser = sysUserService.selectByPrimaryKey(Long.valueOf(editId));
            model.addAttribute("user",sysUser);
        }
        return "user/userEdit";
    }

    @RequestMapping(value = "/editUser")
    @ResponseBody
    public String editUser(@RequestParam(value="editId",required=false) String editId,
                           @RequestParam(name = "userName",defaultValue = "",required = false) String userName,
                           @RequestParam(name = "passWord",defaultValue = "",required = false) String passWord,
                           @RequestParam(name = "userPhone",defaultValue = "",required = false) String userPhone,
                           @RequestParam(name = "address",defaultValue = "",required = false) String address,
                           @RequestParam(name = "birthday",defaultValue = "",required = false) String birthday,
                           @RequestParam(name = "realName",defaultValue = "",required = false) String realName,
                           @RequestParam(name = "email",defaultValue = "",required = false) String email) throws ParseException {

        SysUser sysUser = new SysUser();
        sysUser.setUserName(userName);
        sysUser.setUserPhone(userPhone);
        sysUser.setAddress(address);
        sysUser.setEmail(email);
        sysUser.setBirthday(DateTimeUtil.StringToDate(birthday));
        sysUser.setRealName(realName);
        try {
            if (StringUtils.trimToNull(editId) != null){
                sysUser.setId(Long.valueOf(editId));
                sysUserService.updateByPrimaryKeySelective(sysUser);
                return "{\"msg\":\"修改成功\"}";
            }else {
                sysUser.setPassWord(passWord);
                sysUserService.insertSelective(sysUser);
                return "{\"msg\":\"添加成功\"}";
            }
        }catch (NullPointerException e){
            e.printStackTrace();
                return "{\"msg\":\"系统错误\"}";
        }
    }

    @RequestMapping(value = "/delUser")
    @ResponseBody
    public String delUser(@RequestParam(value="delId",required=false) Long delId){
        sysUserService.deleteByPrimaryKey(delId);
        return "{\"msg\":\"删除成功\"}";
    }


    @RequestMapping(value = "/getRolesByCurrentUser")
    @ResponseBody
    public List<RoleOnUser> getRolesByCurrentUser(){
        List<SysRole> roles = sysRoleService.selectRoles();
        Subject currentUser = SecurityUtils.getSubject();
        String userName = currentUser.getSession().getAttribute("currentUser").toString();
        List<SysRole> roleUser = sysRoleService.selectSysRolesByUserName(userName);
        List<RoleOnUser> roleOnUsers = new ArrayList<>();
        for (SysRole role : roles){
            for (SysRole ru : roleUser){
                RoleOnUser r = new RoleOnUser();
                if (role.getId() == ru.getId()){
                    r.setRoleId(role.getId());
                    r.setRoleName(role.getRoleName());
                    r.setStatus(1);
                }else {
                    r.setRoleId(role.getId());
                    r.setRoleName(role.getRoleName());
                    r.setStatus(0);
                }
                roleOnUsers.add(r);
            }
        }
        return roleOnUsers;
    }

}
